package edu.psu.geovista.app.pcp.plot.control.tools;

import edu.psu.geovista.app.pcp.plot.gui.BasicPcpUI;
import edu.psu.geovista.app.pcp.plot.layer.AxisLayer;
import edu.psu.geovista.app.plot.axis.AxisList;
import edu.psu.geovista.app.plot.axis.YAxis;
import edu.psu.geovista.app.plot.layer.Layer;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.util.Iterator;

/**
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Java source file for the interface Classifier
 * Copyright (c), 2002, GeoVISTA Center
 * Licensed under Simplified BSD License
 *
 * The cmd always get mouse input from MouseObserver no matter how MouseObserver switch the cmd
 * 
 * User: jin chen
 * Date: Dec 14, 2003
 * Time: 5:22:33 PM
 */
public class DefaultMouseTool extends BasicMouseTool{
    DataSelectTool dscd;
    YAxis selectedAxis;//selected axis

    JLabel tmplb=new JLabel();

    public DefaultMouseTool(BasicPcpUI ui) {
        super(ui);
        dscd=(DataSelectTool) this.getCmd(BasicTool.DataSelectT );

    }
    public void mousePressed(MouseEvent e) {
        int x=e.getX() ;
        int y=e.getY() ;
        YAxis axis= this.getContainingAxis(x,y);
        this.setSelectedAxis(axis);
        /*YAxis axis=selectedAxis;
        if(axis!=null){
            axis.setSelected(true);
        }
        AxisLayer axLyr=(AxisLayer) this.getLayer(Layer.AXIS_LAYER );
        axLyr.repaintLayer() ;
        this.repaintPlot() ;*/
    }
     public void mouseDragged(MouseEvent e) {

     }
     public void mouseMoved(MouseEvent e) {
          dscd.mouseMoved(e);//for highlight purpose, dscd always get mouse move input
          //show column description
          showDescriptionOnOff(e);

     }
     public void mouseReleased(MouseEvent e) {
        this.setSelectedAxis(null);//select nothing

     }
     /**
      * show description only when mouse move over axis's header
      * @param e
      */
     private void showDescriptionOnOff(MouseEvent e){
        int x=e.getX() ;
        int y=e.getY() ;


        /* YAxis axis = this.getAxisBeingMouseOvered(x,y);

         if(axis!=null ){
             boolean showDescription=axis.isShowDescription() ;
             if(!showDescription){
             axis.setShowDescription(true);
             AxisLayer axLyr=(AxisLayer) this.getLayer(Layer.AXIS_LAYER );
             axLyr.repaintLayer() ;
             this.repaintPlot() ;
             }
             //System.out.println("Axis:"+axis.getName() +" show description");
         }
         else{//remove description
             AxisList yaxisList = getUI().getPlot().getYaxisList();
             Iterator iter=yaxisList.iterator() ;
             boolean needrepaint=false;
             while(iter.hasNext()){
               YAxis yaxis = (YAxis)iter.next();
               if(yaxis.isShowDescription() ){
                   //even one description exist, need repaint to remove it
                     needrepaint=true;
               }
               yaxis.setShowDescription(false);
             }
             if(needrepaint){
                 AxisLayer axLyr=(AxisLayer) this.getLayer(Layer.AXIS_LAYER );
             axLyr.repaintLayer() ;
             this.repaintPlot() ;
             }
         }*/
     }
     private void setSelectedAxis(YAxis axis){


        if(axis!=null){
            axis.setSelected(true);
        }
        else{
            if(selectedAxis!=null)
                selectedAxis.setSelected(false);

        }
        selectedAxis=axis;
        AxisLayer axLyr=(AxisLayer) this.getLayer(Layer.AXIS_LAYER );
        axLyr.repaintLayer() ;
        this.repaintPlot() ;
     }
    /**
     *
     * @param x
     * @param y
     * @return  an YAxis whose root contain the point(x,y)
     */
    public YAxis getContainingAxis(int x, int y){

        for (Iterator iterator = this.getAxisList().iterator(); iterator.hasNext();) {
            YAxis axis = (YAxis) iterator.next();
            if(axis.footerContain(x,y) )
                return axis;

        }
        return null;
    }

     /**
     *
     * @param x
     * @param y
     * @return   
     */
    public YAxis getAxisBeingMouseOvered(int x, int y){
         AxisList yaxisList = getUI().getPlot().getYaxisList();
         for (Iterator iterator = yaxisList.iterator(); iterator.hasNext();) {
            YAxis axis = (YAxis) iterator.next();
            if(axis.isDescriptionAreaContain(x,y) )
                return axis;

        }
        return null;
    }
}
